<?php
namespace Plu\Library\Models\Platform\Tag;

use Phalcon\Mvc\Model,
    Phalcon\Db\Column;

class TagMap extends Model {

    public $Id;

    public $TagId;

    public $TagName;

    public $ItemId;

    public $ItemType;

    public $Path;

    const ITEM_TYPES= array(
        'COLLECTION' => 1,
        'CATEGORY'   => 2,
        'ROOM'       => 3
    );

    public function initialize() {
        $this->setReadConnectionService('db.longzhulivereadonly.slave');
    }

    public function getSource() {
        return 'TagMap';
    }

    public static function getItemIdsByItemNames($itemNames, $ItemType) {
        if (!is_array($itemNames)) {
            $itemNames = array($itemNames);
        }

        if (empty($itemNames)) {
            return [];
        }

        $sql = array();
        $len = count($itemNames);
        $bind = array();
        $bindTypes = array();

        for ($c = 0; $c < $len; $c++) {
            $p = "n{$c}";
            $sql[] = " TagName = :{$p}: ";
            $bind[$p] = $itemNames[$c];
            $bindTypes[$p] = Column::BIND_PARAM_STR;
        }

        $sql = '(' . join(' OR ', $sql) . ')';
        $sql .= " AND ItemType = :itemType:";
        $bind['itemType'] = $ItemType;
        $bindTypes['itemType'] = Column::BIND_PARAM_INT;

        $items = self::find(array(
            $sql,
            'columns'   => 'ItemId',
            'bind'      => $bind,
            'bindTypes' => $bindTypes
        ));

        $items = $items->toArray();

        $items = array_column($items, 'ItemId');
        return $items;
    }
}
?>
